package com.sansan.qiangji.controller.base;

import com.sansan.qiangji.VO.base.stock.StockVO;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.*;
import com.sansan.qiangji.realize.base.ShelveReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

@CrossOrigin()
@RestController
@RequestMapping("/shelve")
@Slf4j
public class ShelveController {
    @Autowired
    private ShelveReal shelveReal;


    @PostMapping("/addShelve")
    public ResultVO<Object> addShelve(@Valid @RequestBody ShelveAddForm shelveAddForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【物品新增】参数不正确，shelveAddForm={}",shelveAddForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        shelveReal.addShelve(shelveAddForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/getShelveList")
    public ResultVO<Object> getShelveList(@Valid @RequestBody ShelveListForm form, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【物品查询】参数不正确，shelveAddForm={}",form);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultVOUtil.success(shelveReal.shelveList(form));
    }

    @PostMapping("/editShelve")
    public ResultVO<Object> editShelve(@Valid @RequestBody ShelveEditForm shelveEditForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【物品修改】参数不正确，shelveAddForm={}",shelveEditForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        shelveReal.editShelve(shelveEditForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/deleteShelve")
    public ResultVO<Object> deleteShelve(@RequestParam(value = "shelveId") Integer shelveId){
        shelveReal.deleteShelve(shelveId);
        return ResultVOUtil.success(null);
    }
    //推荐货位
    @PostMapping("/recommendEmptyPosition")
    public ResultVO<Object> recommendEmptyPosition(@RequestParam(value = "shelveName") String shelveName){
        StockVO stockVO = shelveReal.recommendEmptyPosition(shelveName);
        return ResultVOUtil.success(stockVO);
    }

    //货位状态修改
    @PostMapping("/changeFreezeStatus")
    public ResultVO<Object> changeFreezeStatus(@RequestBody StockChangeFreezeStatusForm freezeStatusForm){
        shelveReal.changeFreezeStatus(freezeStatusForm);
        return ResultVOUtil.success(null);

    }


    //分组统计库存
    @PostMapping("/groupCountByExample")
    public ResultVO<Object> groupCountByExample(@RequestBody StockListForm stockListForm){
        return ResultVOUtil.success( shelveReal.groupCountByCondition(stockListForm));


    }



}
